/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.service.impl;

import me.zhengjie.modules.ejweixin.domain.NgVideoCenter;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgVideoCenterService;
import me.zhengjie.modules.ejweixin.domain.vo.NgVideoCenterQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgVideoCenterMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cy
* @date 2025-04-24
**/
@Service
@RequiredArgsConstructor
public class NgVideoCenterServiceImpl extends ServiceImpl<NgVideoCenterMapper, NgVideoCenter> implements NgVideoCenterService {

    private final NgVideoCenterMapper ngVideoCenterMapper;

    @Override
    public PageResult<NgVideoCenter> queryAll(NgVideoCenterQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngVideoCenterMapper.findAll(criteria, page));
    }

    @Override
    public PageResult<NgVideoCenter> getVideoList(NgVideoCenterQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(ngVideoCenterMapper.findAll(criteria, page));
    }

    @Override
    public List<NgVideoCenter> queryAll(NgVideoCenterQueryCriteria criteria){
        return ngVideoCenterMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgVideoCenter resources) {
        resources.setCreateTime(DateUtil.getTimeStamp());
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NgVideoCenter resources) {
        NgVideoCenter ngVideoCenter = getById(resources.getId());
        ngVideoCenter.copy(resources);
        resources.setCreateTime(DateUtil.getTimeStamp());
        resources.setUpdateTime(DateUtil.getTimeStamp());
        saveOrUpdate(ngVideoCenter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NgVideoCenter> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgVideoCenter ngVideoCenter : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("视频名称", ngVideoCenter.getVideoTitle());
            map.put("视频地址", ngVideoCenter.getVideoPath());
            map.put("视频类型", ngVideoCenter.getVideoType());
            map.put("观看次数", ngVideoCenter.getNumberViews());
            map.put("创建者", ngVideoCenter.getCreateBy());
            map.put("更新者", ngVideoCenter.getUpdateBy());
            map.put("创建日期", ngVideoCenter.getCreateTime());
            map.put("更新时间", ngVideoCenter.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}